namespace Brjnk.GpxExplorer.Data.GPX
{
    using System;
    using System.Linq;
    using System.Windows.Media;
    using Brjnk.GpxExplorer.Data.GPX.SerializationClasses;

    /// <summary>
    /// </summary>
    public class GpxTrack : Track, IGpxObject
    {
        private trkType data;

        public GpxTrack(trkType data)
        {
            this.data = data;
            Segments = new MVVM.ObservableCollectionEx<TrackSegment>();

            foreach (var p in data.trkseg)
            {
                Segments.Add(new GpxTrackSegment(p));
            }
        }

        public GpxTrack() : this(new trkType()) { }

        public object GetUnderlayingObject()
        {
            data.trkseg.Clear();
            foreach (var p in Segments)
            {
                trksegType s;
                if (p is GpxTrackSegment)
                {
                    s = (trksegType)(p as GpxTrackSegment).GetUnderlayingObject();
                }
                else
                {
                    s = (trksegType)p.ConvertTo<GpxTrackSegment>().GetUnderlayingObject();
                }
                data.trkseg.Add(s);
            }

            return data;
        }

        public override string Name
        {
            get
            {
                return data.name;
            }
            set
            {
                data.name = value;
                OnPropertyChanged("Name");
            }
        }

        public override string Comment
        {
            get
            {
                return data.cmt;
            }
            set
            {
                data.cmt = value;
                OnPropertyChanged("Comment");
            }
        }

        public override Link Link
        {
            get
            {
                if (data.link == null || data.link.Count == 0) return null;
                return new Link(data.link.First().text, data.link.First().href);
            }
            set
            {
                if (value == null) data.link = null;
                else
                {
                    linkType l = new linkType();
                    l.href = value.Url;
                    l.text = value.Text;
                    if (data.link.Count == 0) data.link.Add(l);
                    else data.link[0] = l;
                }
                OnPropertyChanged("Link");
            }
        }

        public override string Description
        {
            get
            {
                return data.desc;
            }
            set
            {
                data.desc = value;
                OnPropertyChanged("Description");
            }
        }

        private Color? color;

        public override Color? Color
        {
            get
            {
                if (color != null) return color;
                if (data.extensions == null || data.extensions.trackExtension == null) return null;
                else if (data.extensions.trackExtension.DisplayColorSpecified == false) return null;
                else return Converter.StringToColor(data.extensions.trackExtension.DisplayColor);
            }
            set
            {
                color = value;

                if (value.HasValue)
                {
                    if (data.extensions == null) data.extensions = new trackExtensionsType();
                    if (data.extensions.trackExtension == null) data.extensions.trackExtension = new TrackExtension_t();
                    data.extensions.trackExtension.DisplayColorSpecified = true;
                    data.extensions.trackExtension.DisplayColor = (DisplayColor_t)Enum.Parse(typeof(DisplayColor_t), Converter.ColorToString(value.Value));
                }
                else
                {
                    if (data.extensions != null && data.extensions.trackExtension != null)
                        data.extensions.trackExtension.DisplayColorSpecified = false;
                }
                OnPropertyChanged("Color");
            }
        }
    }
}